# -*- coding: utf-8 -*-
class ProjectfileController < ApplicationController
  # ヘルパ定義
  include ApplicationHelper

  ###########################################################
  # メソッド：dlg_indexアクション
  # 概　　要：タスク編集ダイアログのためのデフォルトアクション
  ###########################################################
  def dlg_index
  end

  # upload_check
  # 同名のファイルがないかどうかをチェックする
  # 
  def upload_check
    project_id       = params[:dlg_fil_upload_project_id].nil? ? '' : params[:dlg_fil_upload_project_id]
    parent_node      = params[:dlg_fil_upload_parent_node].nil? ? '' : params[:dlg_fil_upload_parent_node]
    upload_file_name = params[:dlg_fil_upload_upload_file_name].nil? ? '' : params[:dlg_fil_upload_upload_file_name]

    # パラメータチェック
    if project_id == "" || parent_node == "" || upload_file_name == ""
      render :text => result_for_json(false, app_localized_message(:error, :error_parameter), {})
      return
    end

    iconv = Iconv.new($FILESYSTEM_ENCODING, $SYSTEM_ENCODING)
    parent_path = iconv.iconv(parent_node)
    if valid_node?(project_id, parent_path)
      
      # パスの取得
      rootpath    = get_project_files_root(project_id)
      target_path = rootpath + iconv.iconv(parent_node)
      
      # ファイル名の取得
      file      = params[:dlg_fil_upload_upload_file]
      fname     = file.original_filename
      fname     = iconv.iconv(fname)
      fullfname = target_path + fname

      result = {:confirm=>""}
      message = ""
      success = true

      # ファイル名存在チェック
      if FileTest.exist?(fullfname)
        # ディレクトリ？
        if FileTest.directory?(fullfname)
          # 同名エラー
          message = app_localized_message(:error, :file_already_exist)
          success = false
        else
          # 上書き確認
          result[:confirm]  = app_localized_message(:confirm, :file_already_exist)
        end
      end
    else
      success = false
      message = 'Permission Denied'
      result  = {:confirm=>""}
    end
    render :text => result_for_json( success, message, result )
  end

  
  def upload
    project_id  = params[:dlg_fil_upload_project_id].nil? ? '' : params[:dlg_fil_upload_project_id]
    parent_node = params[:dlg_fil_upload_parent_node].nil? ? '' : params[:dlg_fil_upload_parent_node]
    @callback   = params[:dlg_fil_upload_callback_function].nil? ? '' : params[:dlg_fil_upload_callback_function]

    iconv       = Iconv.new($FILESYSTEM_ENCODING, $SYSTEM_ENCODING)
    parent_path = iconv.iconv(parent_node)
    if valid_node?(project_id, parent_path)
      rootpath    = get_project_files_root(project_id)
      target_path = rootpath + iconv.iconv(parent_node)
      
      file      = params[:dlg_fil_upload_upload_file]
      fname     = file.original_filename
      fname     = iconv.iconv(fname)
      fullfname = target_path + fname
      
      if fullfname != "" then
        if FileTest.exist?(fullfname)
          File.delete(fullfname)
        end
        File.open(fullfname, "wb"){ |f| f.write(file.read) }
      end
    
      @success = true
      @message = ""
      @result = {}
      
      render :text => result_for_json(@success, @message, @result)
    else
      render :text => 'Permission Denied'
    end
  end

  def download
    project_id  = params[:projectfile_download_project_id].nil? ? '' : params[:projectfile_download_project_id]
    target_file = params[:projectfile_download_target_file].nil? ? '' : params[:projectfile_download_target_file]
    browser_ie  = params[:projectfile_download_browser_ie].nil? ? '' : params[:projectfile_download_browser_ie]
    
    iconv    = Iconv.new($FILESYSTEM_ENCODING, $SYSTEM_ENCODING)
    rootpath = get_project_files_root(project_id)

    fname = target_file
    fname = iconv.iconv(fname)
    if valid_node?(project_id, File.dirname(fname))
      fullfname = rootpath + fname
      fname = File.basename(fname)
      if browser_ie == "1"
        iconv = Iconv.new("SHIFT_JIS", $FILESYSTEM_ENCODING)
        fname = iconv.iconv(fname)
      else
        iconv = Iconv.new($SYSTEM_ENCODING, $FILESYSTEM_ENCODING)
        fname = iconv.iconv(fname)
      end
      send_file fullfname, {:filename=>fname}
    else
      render :text => 'Permission Denied'
    end
  end
  
  def destroy
    project_id  = params[:project_id].nil? ? '' : params[:project_id]
    target_file = params[:target_file].nil? ? '' : params[:target_file]

    iconv     = Iconv.new($FILESYSTEM_ENCODING, $SYSTEM_ENCODING)
    fname = iconv.iconv(target_file)
    if valid_node?(project_id, File.dirname(fname))
      rootpath  = get_project_files_root(project_id)
      fullfname = rootpath + target_file

      result  = {}
      message = ""
      success = true
      fullfname = iconv.iconv(fullfname)
      if fullfname != "" then
        if File.ftype(fullfname) == "directory"
          entries = Dir.entries(fullfname)
          if entries.length > 2
            message = app_localized_message(:error, :entry_file_exists)
            success = false
          else
            Dir.rmdir(fullfname)
          end
        else
          File.delete(fullfname)
        end
      end
      render :text => result_for_json(success, message, result)
    else
      render :text => 'Permission Denied'
    end
  end

  def folder_rename
    project_id = params[:dlg_dir_edit_project_id].nil? ? '' : params[:dlg_dir_edit_project_id]
    folder_id  = params[:folder_id].nil? ? '' : params[:folder_id]
    dir_name   = params[:dlg_dir_edit_dir_name].nil? ? '' : params[:dlg_dir_edit_dir_name]
    
    iconv = Iconv.new($FILESYSTEM_ENCODING, $SYSTEM_ENCODING)
    folder_path = iconv.iconv(folder_id)
    if valid_node?(project_id, folder_path)
      # パスの取得
      rootpath = get_project_files_root(project_id)

      end_s = (rootpath + folder_id).rindex("/")
      target_path = (rootpath + folder_id).slice(0, end_s + 1)

      # ディレクトリ名の取得
      dname = iconv.iconv( dir_name )
      fulldname = target_path + dname

      result  = {}
      message = ""
      success = true

      # 同一ディレクトリ名存在チェック
      if FileTest.exist?(fulldname)
        # 同名エラー
        message = app_localized_message(:error, :folder_already_exist)
        success = false
        render :text => result_for_json(success, message, result)
        return 
      end
      File.rename(rootpath + folder_id, target_path + dir_name)
      render :text => result_for_json(success, message, result)
    else
      render :text => 'Permission Denied'
    end
  end


  def create_dir
    project_id = params[:dlg_dir_edit_project_id].nil? ? '' : params[:dlg_dir_edit_project_id]
    parent_node = params[:dlg_dir_edit_parent_node].nil? ? '' : params[:dlg_dir_edit_parent_node]
    dir_name = params[:dlg_dir_edit_dir_name].nil? ? '' : params[:dlg_dir_edit_dir_name]
    # パラメータチェック
    if project_id == "" || parent_node == "" || dir_name == ""
      render :text => result_for_json( false, app_localized_message(:error, :error_parameter), {} )  
      return
    end

    iconv = Iconv.new($FILESYSTEM_ENCODING, $SYSTEM_ENCODING)
    parent_path = iconv.iconv(parent_node)
    if valid_node?(project_id, parent_path)
      # パスの取得
      rootpath = get_project_files_root(project_id)
      target_path = rootpath + iconv.iconv(parent_node)

      # ディレクトリ名の取得
      dname = iconv.iconv( dir_name )
      fulldname = target_path + dname

      result = {}
      message = ""
      success = true
      # ディレクトリ名存在チェック
      if FileTest.exist?(fulldname)
        # 同名エラー
        message = app_localized_message(:error, :folder_already_exist)
        success = false
        render :text => result_for_json(success, message, result)
        return 
      end
      if fulldname != ""
        FileUtils.mkdir_p(fulldname)
      end
      render :text => result_for_json(success, message, result)
    else
      render :text => 'Permission Denied'
    end
  end

end
